<?php

/**
 * sfWidgetFormJQueryScale
 *
 * @author José Nahuel Cuesta Luengo <ncuesta@cespi.unlp.edu.ar>
 */
class sfWidgetFormJQueryScale extends sfWidgetFormInput
{
  protected function configure($options = array(), $attributes = array())
  {
    parent::configure($options, $attributes);

    $this->addOption('min', 0);
    $this->addOption('max', 100);
    $this->addOption('step', 1);
    $this->addOption('hide_input', false);
  }

  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
    $template = <<<HTML
%input%
<div class="sf_slider" id="%id%_slider"></div>

<script type="text/javascript">
  jQuery('#%id%_slider').slider({
    min:      %min%,
    max:      %max%,
    step:     %step%,
    animate:  'normal',
    value:    %value%,
    change:   function(event, ui) {
      jQuery('#%id%').val(ui.value);
    }
  });

  jQuery('#%id%_slider').slider('option', 'disabled', %disabled%);
</script>
HTML;

    if ($this->getOption('hide_input'))
    {
      $attributes['type'] = 'hidden';
    }

    return strtr($template, array(
      '%input%'    => parent::render($name, $value, $attributes, $errors),
      '%id%'       => $this->generateId($name, $value),
      '%min%'      => $this->getOption('min'),
      '%max%'      => $this->getOption('max'),
      '%step%'     => $this->getOption('step'),
      '%disabled%' => isset($attributes['disabled']) && $attributes['disabled'] ? 'true' : 'false',
      '%value%'    => '' !== trim($value) ? $value : 'null'
    ));
  }

  public function getJavaScripts()
  {
    return array_merge(parent::getJavaScripts(), array('jquery/jquery-1.4.4.min.js', 'jquery/jquery-ui-1.8.7.custom.min.js'));
  }

}